package com.itic.mall.system.productjdstatis.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.itic.appbase.applications.sys.common.service.OutCommonService;
import com.itic.appbase.framework.common.constants.DBConst;
import com.itic.appbase.framework.common.constants.IticConst;
import com.itic.appbase.framework.common.constants.ResponseCode;
import com.itic.appbase.framework.common.controller.BaseController;
import com.itic.appbase.framework.utils.DateHelper;
import com.itic.appbase.framework.utils.StringHelper;
import com.itic.mall.system.productjdstatis.persistence.model.ProductJdStatis;
import com.itic.mall.system.productjdstatis.persistence.model.ProductJdStatisExample;
import com.itic.mall.system.productjdstatis.persistence.model.ProductJdStatisExample.Criteria;
import com.itic.mall.system.productjdstatis.service.ProductJdStatisService;


@Controller
@RequestMapping(value="/system/productjdstatis")
public class ProductJdStatisController
     extends BaseController {
    @Autowired
    private ProductJdStatisService productJdStatisService;

    
    @Autowired
   	protected OutCommonService outCommonService;
       
       //////////////////////////////////////////////////////常量区////////////////////////////////////////////////////////////////
       private static final String MODEL_NAME_CHINA = "京东二合一统计";//中文名
       private static final String MODEL_NAME＿ENGLISH = "productjdstatis";//英文名
       private static final String PAGE_COMMON_PATH = "/mall/system/productjdstatis/productjdstatis";//通用路径
       private static final String COMMON_URL = "/system/productjdstatis/";//通用路径
       

       //////////////////////////////////////////////////////页面跳转区////////////////////////////////////////////////////////////
      
       /*
        * 主页面
        */
       @RequestMapping(value="/init")
       public String init(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Main";
       }
       
       /*
        * 添加页面
        */
       @RequestMapping(value="/initAdd")
       public String initAdd(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Add";
       }
       
       
       /*
        * 导入页面
        */
       @RequestMapping(value="/initImp")
       public String initImp(Model model){
       	model.addAttribute("name_china", MODEL_NAME_CHINA);
       	model.addAttribute("name_english", MODEL_NAME＿ENGLISH);
       	model.addAttribute("common_url", COMMON_URL);
       	return PAGE_COMMON_PATH+"Imp";
       }
       
       
       //////////////////////////////////////////////////////业务代码区////////////////////////////////////////////////////////////
       
       
       /**
        * <p>Title:queryCondition </p>
        * <p>Description:封装查询条件 </p>
        * @param jzdw
        * @param jzdwExample
        * @return
        */
       private ProductJdStatisExample queryCondition(HttpServletRequest request,ProductJdStatis temp) {
       	
       	// 参数处理
           ProductJdStatisExample example = new ProductJdStatisExample();
           // 按创建时间倒序
           example.setOrderByClause("create_time desc");
           // 按排列号顺序
           example.setOrderByClause("reorder desc");
           
           Criteria criteria = example.createCriteria().andIsEffectiveEqualTo( DBConst.TRUE );
         
           //过滤组织机构
           //User user = findUserSession(request);
           //criteria.andOrgCodeEqualTo(user.getOrganizationCode());
           
           // 查询条件
           if ( null != temp ) {
       	       //模糊查询
               if ( StringHelper.isNotBlank( temp.getNote() ) ) {
                   criteria.andNoteLike("%" + temp.getNote() + "%");
               }
               
               if( null != temp.getCreateTime()){
            	   Date createTime = temp.getCreateTime();
            	   String startTime = DateHelper.formatDate(new Date());
            	   criteria.andCreateTimeGreaterThanOrEqualTo(createTime);
            	   String endTime = startTime + " 23:59:59";
            	   Date dateByStringDate = DateHelper.getDateByStringDate(endTime);
            	   criteria.andCreateTimeLessThanOrEqualTo(dateByStringDate);
            	   
               }else{
            	   String date = DateHelper.formatDate(new Date());
            	   Date dateByStringDate = DateHelper.getDateByStringDateYYYYMMDD(date);
            	   criteria.andCreateTimeGreaterThanOrEqualTo(dateByStringDate);
            	   String endTime = date + " 23:59:59";
            	   Date dateByStringDate2 = DateHelper.getDateByStringDate(endTime);
            	   criteria.andCreateTimeLessThanOrEqualTo(dateByStringDate2);
               }
               
           }
           return example;
       }
       
       /**
        * 
        * @param goldType
        * @param request
        * @return
        */
       @RequestMapping(value = "/list")
       @ResponseBody
       public Map<String, Object> list(ProductJdStatis ProductJdStatis, HttpServletRequest request){
           // 声明返回对象
           Map<String, Object> resultMap = new HashMap<String, Object>();
           
           
           ProductJdStatisExample example = this.queryCondition(request,ProductJdStatis);
           // 封装分页参数
           example = this.handlePageQuery(request, example);
           
           
           // 获取信息总条数
           int total = productJdStatisService.count(example);
           
           // 获取数据
           List<ProductJdStatis> list = productJdStatisService.list(example);
           
           // 数据返回处理
           resultMap.put(IticConst.TOTAL_RECORDS, total);
           resultMap.put(IticConst.DISPLAY_RECORDS, total);
           resultMap.put(IticConst.RESULT, ResponseCode.SUCCESS.getCode());
           resultMap.put(IticConst.DATA, list);
           return resultMap;

       }
       
       /**
        * 编辑保存
        * @param ProductJdStatis
        * @return
        */
       @RequestMapping(value="/edit")
       @ResponseBody
       public Map<String,String> edit(String delFileIds , ProductJdStatis ProductJdStatis){
       
       	if( StringHelper.isNotBlank(delFileIds) ){
       		// 删除附件
       		outCommonService.deleteFiles(delFileIds);
       	}
       	
       	 Map<String, String> resultMap = new HashMap<String, String>();
            String result = ResponseCode.SERVER_ERROR.getCode();
            result = productJdStatisService.edit(ProductJdStatis);
            
            resultMap.put(IticConst.RESULT, result);
            return resultMap;
       }
       
       

       /**
        * 删除
        * 
        */
       @RequestMapping(value = "/del")
       @ResponseBody
       public Map<String, String> del(String ids) {
           Map<String, String> resultMap = new HashMap<String, String>();
           String result = ResponseCode.SERVER_ERROR.getCode();

           //批量删除
           if (StringHelper.isNotBlank(ids)) {
           	
               List<String> idList = Arrays.asList(ids.split(","));
               
               ProductJdStatisExample example = new ProductJdStatisExample();
               example.createCriteria().andIdIn(idList);
             
               ProductJdStatis ProductJdStatis = new ProductJdStatis();
               result = productJdStatisService.deleteByExample(ProductJdStatis, example);
           } else {
               result = ResponseCode.PARAM_INVALID.getCode();
           }

           //删除一个
           /*
           if( StringHelper.isNotBlank(id) ){
               ProductJdStatis ProductJdStatis = new ProductJdStatis();
               ProductJdStatis.setId(id);
               result = productJdStatisService.delete(ProductJdStatis);
           }
           */
           resultMap.put(IticConst.RESULT, result);
           return resultMap;
       }
       
       
}